#!/bin/sh
. ./conf/env.sh

LANG=en_US.utf8

readonly SCRIPT_DIR=$(cd $(dirname $0) && pwd)
readonly SCRIPT_FILENAME=$(basename $0)
readonly MOCK_CMD_PREFIX="/usr/bin/mock --configdir=$SCRIPT_DIR/mock"

isoname="${PRODUCTINFO_ISO}.iso"

ks_os=isopkg-ns7.0-jk.ks
ks_installer=isopkg-ns7.0-jk-installer.ks
comps=comps-ns7.xml
confdir=$SCRIPT_DIR/conf

arch=mips64el
outisorc=/build/isosource-ns7
pungi_options="--force --nosource --nodebuginfo --name \"${product_name}-${product_edition}\" --ver \"${product_version}\" --release \"B${product_build_id}\" --bugurl \"http://bugzilla.cs2c.com.cn\" --isfinal --nohash --nogreedy --full-archlist"
work_root=/ns7


# lock file creation and removal
LOCKFILE=$SCRIPT_DIR/`basename $0`.lock
clean_up()
{
    rm -rf $LOCKFILE
    exit 0
}

clean_up_err()
{
    rm -rf $LOCKFILE
    exit 1 #Exit code 1
}

[ -f $LOCKFILE ] && { echo $LOCKFILE exists; exit 0; }
trap clean_up_err 2 9 15
trap clean_up QUIT EXIT
touch $LOCKFILE


xmock_init() {
    $MOCK_CMD_PREFIX -r $1 --init
}

xmock_install() {
    $MOCK_CMD_PREFIX -r $1 --install $2
}

xmock_shell() {
    $MOCK_CMD_PREFIX -r $1 --shell "$2"
}

xmock_copyin() {
    $MOCK_CMD_PREFIX -r $1 --copyin $2 $3
}

xmock_chroot() {
    $MOCK_CMD_PREFIX -r $1 --chroot "$2"
}

xmock_get_root_path() {
    $MOCK_CMD_PREFIX -r $1 --print-root-path
}

cd $SCRIPT_DIR

echo "${GREEN}Welcome to the NeoKylin OS build script${RESET}"
m4 -Dproduct_version_id=$product_version_id \
    -Dproduct_build_id=$product_build_id \
    -Dproduct_type=$product_type \
    -Dproduct_build_date=$product_build_date  conf/config.m4 > addon_config.sh && chmod +x addon_config.sh

xmock_init ns7.0-jk-mips64el
xmock_install ns7.0-jk-mips64el "pungi vim yum-utils"

xmock_shell ns7.0-jk-mips64el "mkdir -p ${work_root}/{conf,pungi,log}"
xmock_copyin ns7.0-jk-mips64el "$confdir/*" ${work_root}/conf

xmock_shell ns7.0-jk-mips64el "rm -rf ${work_root}/rc"
xmock_copyin ns7.0-jk-mips64el $outisorc/notbootfiles-jk ${work_root}/rc
xmock_copyin ns7.0-jk-mips64el addon_config.sh ${work_root}/rc/addon/config.sh

xmock_copyin ns7.0-jk-mips64el "x-post.sh" ${work_root}

echo "${GREEN}Beginning build process${RESET}"
#echo "removing destdir..." 
#rm -rf `pwd`/work
xmock_chroot ns7.0-jk-mips64el-bind-os-repo "cd $work_root && /usr/bin/pungi $pungi_options --cachedir=/var/cache/pungi/os --destdir=${work_root}/pungi/os -c ./conf/$ks_os -G -C"

xmock_chroot ns7.0-jk-mips64el-bind-installer-repo "cd $work_root && /usr/bin/pungi $pungi_options --cachedir=/var/cache/pungi/installer --destdir=${work_root}/pungi/installer -c ./conf/$ks_installer -G -C"
xmock_chroot ns7.0-jk-mips64el-bind-installer-repo "cd $work_root && /usr/bin/pungi $pungi_options --cachedir=/var/cache/pungi/installer --destdir=${work_root}/pungi/installer -c ./conf/$ks_installer -B"

xmock_chroot ns7.0-jk-mips64el "cd ${work_root}/pungi/os/${product_version}/$arch/os/ &&  rm -rf repodata && createrepo -g ${work_root}/conf/$comps ."
xmock_chroot ns7.0-jk-mips64el "cd ${work_root}/pungi; for d in Packages repodata repoview; do rsync -arz --delete os/${product_version}/${arch}/os/\$d/ installer/${product_version}/${arch}/os/\$d/; done"

xmock_chroot ns7.0-jk-mips64el "cd ${work_root} && sh ./x-post.sh 2>&1 | tee ./log/post.log"

xmock_chroot ns7.0-jk-mips64el "mkisofs -input-charset utf-8 -L -l -J -R -v -T -m repoview -V ${product_name}-${product_edition}-${product_version}-Loongson -o /$isoname ${work_root}/pungi/installer/${product_version}/$arch/os/ && implantisomd5 --force /$isoname && md5sum /$isoname > /$isoname.md5"

result_dir=$(xmock_get_root_path ns7.0-jk-mips64el)
echo "${GREEN}ISO saved to $result_dir$isoname ${RESET}"

exit 0
